import { CouponType } from '@/helpers/constants/coupon'
import type { APIGoodsDetail, APIGoodsListItem, APIRecordListItem } from '@/types/api/mall'
import type {
  CouponItem,
  GoodsDetail,
  GoodsDiscountItem,
  GoodsItem,
  JumpDiscountItem,
  OrderRecordItem,
  RecordItem,
} from '@/types/mall'
import JumpCoinIcon from '@/assets/images/icon_coin.png'
import JumpCouponIcon from '@/assets/images/icon_coupon.png'
import { formatDate } from '@/utils/date'

export class MallAdapter {
  /** 商品列表转本地 */
  static fromAPIGoodsToGoods(v: APIGoodsListItem[]): GoodsItem[] {
    return Array.from(v, (o) => {
      const discounts: GoodsDiscountItem[] = []
      let vipPrice = parseFloat(o.originalPrice)
      if (o.couponPrice) {
        vipPrice = parseFloat((vipPrice - parseFloat(o.couponPrice)).toFixed(6))
        discounts.push({
          label: '券',
          value: `￥${o.couponPrice}`,
        })
      }
      if (o.commissionPrice) {
        vipPrice = parseFloat((vipPrice - parseFloat(o.commissionPrice)).toFixed(6))
        discounts.push({
          label: '购物金',
          value: `￥${o.commissionPrice}`,
        })
      }
      return {
        id: o.id?.toString(),
        goodsId: o.goodsId,
        cover: o.mainPic,
        name: o.title,
        price: o.originalPrice,
        vipPrice: vipPrice.toString(),
        discounts,
        couponPrice: parseFloat(o.couponPrice),
        commissionPrice: parseFloat(o.commissionPrice),
      }
    })
  }

  /** 商品详情转本地 */
  static fromAPIGoodsDetailToDetail(v: APIGoodsDetail): GoodsDetail {
    const r: GoodsDetail = {
      imgs: v.imgs || [],
      goodsId: v.goodsId,
      title: v.title,
      price: v.price,
      originalPrice: v.originalPrice,
      couponPrice: v.couponPrice,
      commissionPrice: v.commissionPrice,
      coupons: [],
      discounts: [],
    }
    const coupons: CouponItem[] = []
    const discounts: GoodsDiscountItem[] = []
    if (v.couponPrice) {
      coupons.push({
        amount: v.couponPrice.toString(),
        title: '优惠券',
        btnText: '立即领取',
        type: CouponType.CashCoupon,
      })
      discounts.push({
        label: '券',
        value: `￥${v.couponPrice}`,
      })
    }
    if (v.commissionPrice) {
      coupons.push({
        amount: v.commissionPrice.toString(),
        title: '购物金抵扣',
        btnText: '',
        type: CouponType.GiftDeduction,
      })
      discounts.push({
        label: '购物金',
        value: `￥${v.commissionPrice}`,
      })
    }
    r.coupons = coupons
    r.discounts = discounts
    return r
  }

  /** 列表商品转详情 */
  static fromAPIGoodsToDetail(v: GoodsItem): GoodsDetail {
    const r: GoodsDetail = {
      imgs: [],
      goodsId: v.goodsId,
      title: v.name,
      price: v.vipPrice,
      originalPrice: parseFloat(v.price),
      couponPrice: v.couponPrice,
      commissionPrice: v.commissionPrice,
      coupons: [],
      discounts: [],
    }
    let price = parseFloat(v.price)
    const imgs: string[] = []
    const coupons: CouponItem[] = []
    const discounts: GoodsDiscountItem[] = []
    if (v.cover) {
      imgs.push(v.cover)
    }
    if (v.couponPrice) {
      price = parseFloat((price - v.couponPrice).toFixed(6))
      coupons.push({
        amount: v.couponPrice.toString(),
        title: '优惠券',
        btnText: '立即领取',
        type: CouponType.CashCoupon,
      })
      discounts.push({
        label: '券',
        value: `￥${v.couponPrice}`,
      })
    }
    if (v.commissionPrice) {
      price = parseFloat((price - v.commissionPrice).toFixed(6))
      coupons.push({
        amount: v.commissionPrice.toString(),
        title: '购物金抵扣',
        btnText: '',
        type: CouponType.GiftDeduction,
      })
      discounts.push({
        label: '购物金',
        value: `￥${v.commissionPrice}`,
      })
    }
    r.price = Math.max(0, price).toFixed(2)
    r.imgs = imgs
    r.coupons = coupons
    r.discounts = discounts
    return r
  }

  /** 处理订单购物金记录 */
  static fromAPIRecordsToRecords(v: APIRecordListItem[]): OrderRecordItem[] {
    return Array.from(v, (o) => ({
      id: o.id.toString(),
      orderTime: o.createtime,
      date: formatDate(o.createtime * 1000, 'YYYY-MM'),
      day: formatDate(o.createtime * 1000, 'MM-DD HH:mm'),
      action: o.memo,
      actionDetail: '',
      actionColor: '',
      actionAmount: `抵扣:￥${o.money}`,
      balance: o.after,
      userId: o.user_id.toString(),
    }))
  }

  /** 格式化购物金显示列表 */
  static fromOrderRecordsToList(v: OrderRecordItem[]): RecordItem[] {
    return v.reduce((a, c) => {
      const idx = a.findIndex((o) => o.date === c.date)
      if (idx > -1) {
        a[idx].list.push(c)
      } else {
        a.push({
          id: c.id,
          date: c.date,
          list: [c],
        })
      }
      return a
    }, [] as RecordItem[])
  }

  /** 计算商品优惠 */
  static fromGoodsDetailToDiscounts(v: GoodsDetail): {
    discount: number
    list: JumpDiscountItem[]
  } {
    const list: JumpDiscountItem[] = []
    let discount = 0
    if (v.commissionPrice) {
      list.push({
        text: '购物金',
        icon: JumpCoinIcon,
        amount: `￥${v.commissionPrice}`,
      })
      discount = parseFloat((discount + v.commissionPrice).toFixed(6))
    }
    if (v.couponPrice) {
      list.push({
        text: '领券',
        icon: JumpCouponIcon,
        amount: `￥${v.couponPrice}`,
      })
      discount = parseFloat((discount + v.couponPrice).toFixed(6))
    }
    return {
      discount,
      list,
    }
  }
}
